

//
//  YZG_MoreCityController.m
//  Play
//
//  Created by 杨志刚 on 15/9/15.
//  Copyright (c) 2015年 杨志刚. All rights reserved.
//

#import "GMCitys.h"
#import "GMHCitysModel.h"
#import <AFNetworking.h>
#import "ViewController.h"
#import "DKNightVersion.h"

@interface GMCitys ()<UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *dataCityArray;
@property (nonatomic, retain) NSMutableArray *bigArray;
@property (nonatomic, retain) NSMutableArray *indexArray;
@property (nonatomic, retain) UISearchBar *search;
@end

@implementation GMCitys
- (void)dealloc
{
    [_search release];
    [_dataCityArray release];
    [_tableView release];
    [_bigArray release];
    [_indexArray release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.dataCityArray = [NSMutableArray array];
        self.bigArray = [NSMutableArray array];
        self.indexArray = [NSMutableArray array];
    }
    return self;
}
- (void)getData
{
//    [HUDManager showLoadingHUDView:self.tableView];
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSString *strurl = @"http://appapi.yaochufa.com/v2/Position/GetCityList?system=android&machineCode=ffffffff-9317-02f2-ffff-fffff60c2478&channel=xiaomimarket&version=4.3.1";
    [manager GET:strurl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        self.dataCityArray = [GMHCitysModel baseModelArry:dic[@"data"][@"positionCity"]];
       
        NSArray *letter = @[@"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", @"N",@"O", @"P", @"Q", @"R", @"S", @"T",@"U", @"V", @"W", @"X", @"Y", @"Z"];
        
        for (int i = 0; i < 26; i++) {
            
            NSMutableArray *tempArray = [NSMutableArray array];
            for (GMHCitysModel *model in _dataCityArray) {
                if (![model.pinYinName isEqualToString:@""] && [[[model.pinYinName substringToIndex:1] capitalizedString] isEqualToString:letter[i]]){
                    [tempArray addObject:model.cityNameAbbr];
                    
                }
            }
            if (tempArray.count > 0) {
                NSDictionary *dic = [NSDictionary dictionaryWithObject:tempArray forKey:letter[i]];
                [self.indexArray addObject:letter[i]];
                [self.bigArray addObject:dic];
            }
        }
        if (_bigArray.count > 0) {
//            [HUDManager hideHUDView];
            [_tableView reloadData];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"城市列表";
    [self createTableView];
    [self getData];
    
    _search = [[UISearchBar alloc]initWithFrame:CGRectMake(20, 0, self.view.frame.size.width - 40, 40)];
    self.navigationItem.titleView = self.search;
    _search.placeholder = @"搜索目的地";
    self.search.barStyle = UIBarStyleDefault;
    [_search release];
    _search.delegate = self;
    
}
- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    _tableView.sectionIndexColor = [UIColor colorWithWhite:0.653 alpha:1.000];
//        _tableView.sectionIndexColor = [UIColor clearColor];
    _tableView.sectionIndexBackgroundColor = [UIColor clearColor];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorInset = UIEdgeInsetsMake(0, 15, 0 , 0);
    UIView *view = [[UIView alloc] init];
    _tableView.tableFooterView = view;
    [view release];
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"positionCityCell"];

    self.tableView.nightBackgroundColor = [UIColor blackColor];
    self.tableView.nightSeparatorColor = [UIColor darkGrayColor];
    
    [self.view addSubview:_tableView];
    [_tableView release];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"positionCityCell"];
        
        NSArray *array = [_bigArray[indexPath.section ] objectForKey:_indexArray[indexPath.section ]];
    
    [DKNightVersionManager addClassToSet:self.class];
    cell.nightBackgroundColor = UIColorFromRGB(0x343434);
        cell.textLabel.text = array[indexPath.row];
        cell.textLabel.textColor = [UIColor colorWithWhite:0.386 alpha:1.000];
        return cell;

}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
        return 44;
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (_bigArray.count > 0) {
        
        return [_bigArray count];
        
    }else{
        return 0;
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
        
        return [[_bigArray[section ] objectForKey:_indexArray[section]] count];
    
    
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    
        
        return _indexArray[section ];
    
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    NSMutableArray *array = [NSMutableArray arrayWithArray:_indexArray];
    
    return array;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        NSArray *array = [_bigArray[indexPath.section] objectForKey:_indexArray[indexPath.section]];
        
        ViewController *clickCity = [[ViewController alloc] init];
        clickCity.cityNameAbbr = array[indexPath.row];
    self.block(clickCity.cityNameAbbr);
    NSLog(@"%@", clickCity.cityNameAbbr);
    [self.navigationController popToRootViewControllerAnimated:YES];
      [clickCity release];


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end

////
////  GMCitys.m
////  
////
////  Created by 郭明皓 on 15/10/28.
////
////
//
//#import "GMCitys.h"
//#import <AFNetworking.h>
//#import "GMHCitysModel.h"
//#import "ViewController.h"
//@interface GMCitys ()<UITableViewDataSource, UITableViewDelegate>
//@property (nonatomic, retain)UITableView *tableView;
//@property (nonatomic, retain)NSMutableArray *citysArr;
//@property (nonatomic, retain)NSArray *hotCityArr;
//@property (nonatomic, retain)NSArray *CitySectionArr;
//@property (nonatomic, retain)NSMutableArray *zoneArr;
//@property (nonatomic, retain)NSMutableArray *tableArr;
//@end
//
//@implementation GMCitys
//
//- (void)dealloc
//{
//    [_CitySectionArr release];
//    [_hotCityArr release];
//    [_tableView release];
//    [_citysArr release];
//    
//    [super dealloc];
//}
//- (void)viewDidLoad {
//    [super viewDidLoad];
//    // Do any additional setup after loading the view.
//    self.view.backgroundColor = [UIColor whiteColor];
//    
//    self.tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
//    [self.view addSubview:self.tableView];
//    [_tableView release];
//    self.tableView.delegate = self;
//    self.tableView.dataSource = self;
//    self.tableView.rowHeight = 50;
//    
//    self.title = @"";
//    self.CitySectionArr = @[@"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"J", @"K", @"L", @"M", @"N", @"P", @"Q", @"R", @"S", @"T", @"W", @"X", @"Y", @"Z"];
//    [self create];
//    
//}
//- (void)create
//{
//    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//    NSString *strurl = @"http://appapi.yaochufa.com/v2/Position/GetCityList?system=android&machineCode=ffffffff-9317-02f2-ffff-fffff60c2478&channel=xiaomimarket&version=4.3.1";
//    [manager GET:strurl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        NSDictionary *dic = responseObject;
//        self.citysArr = [GMHCitysModel baseModelArry:dic[@"data"][@"positionCity"]];
//        self.hotCityArr = [GMHCitysModel baseModelArry:dic[@"data"][@"hotCity"]];
//        
//        [self.citysArr sortUsingComparator:^NSComparisonResult(GMHCitysModel *obj1, GMHCitysModel *obj2) {
//            obj1.pinYinName = obj1.pinYinName.uppercaseString;
//            obj2.pinYinName = obj2.pinYinName.uppercaseString;
//            return [obj1.pinYinName compare:obj2.pinYinName];
//            
//        }];
//        [self.citysArr removeObjectAtIndex:0];
//        
//        self.zoneArr = [NSMutableArray array];
//        
//        self.tableArr = [NSMutableArray array];
//
//        NSInteger count = 0;
//        for (NSInteger i = 0; i < self.citysArr.count; i++) {
//            if ([[[self.citysArr[i] pinYinName] substringToIndex:1] isEqualToString:self.CitySectionArr[self.zoneArr.count]]) {
//            count++;
////                [self.tableArr addObject:[self.citysArr[i] cityNameAbbr]];
//                NSLog(@"%ld", count);
//                NSLog(@"%ld", self.zoneArr.count);
//                if (i == self.citysArr.count - 1) {
//                    NSNumber *number = [NSNumber numberWithInteger:count];
//                    [self.zoneArr addObject:number];
////                            [self.zoneArr addObject:self.tableArr];
//                }
//            } else {
//                i--;
//
//                [self.zoneArr addObjectsFromArray:self.tableArr];
//                NSLog(@"%@", self.zoneArr);
//                NSNumber *number = [NSNumber numberWithInteger:count];
//                [self.zoneArr addObject:number];
//                count = 0;
////                [self.zoneArr addObject:self.tableArr];
////                [self.tableArr removeAllObjects];
//                
//            }
//        }
//        
//        NSLog(@"%@", self.zoneArr);
////        NSLog(@"2222222%@", self.tableArr);
//        [self.tableView reloadData];
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        NSLog(@"城市列表初始化失败");
//    }];
//}
//- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
//{
////    NSArray *arr = self.zoneArr[section];
//
//    return self.citysArr.count;
//}
////- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
////{
////    return self.zoneArr.count;
////}
////- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
////{
////
////    return self.CitySectionArr[section];
////}
//- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    static NSString *strs = @"citys";
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strs];
//    if (!cell) {
//        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strs];
//        
//    }
//
//    GMHCitysModel *city = self.citysArr[indexPath.row];
//    cell.textLabel.text = city.cityNameAbbr;
//    
//    
//    
//    return cell;
//}
//#pragma mark tableview点击事件
//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    ViewController *views = [[ViewController alloc]init];
//    [self.navigationController popToRootViewControllerAnimated:YES];
//    GMHCitysModel *model = self.citysArr[indexPath.row];
//    views.cityNameAbbr = model.cityNameAbbr;
//    
//    
//}
//- (void)didReceiveMemoryWarning {
//    [super didReceiveMemoryWarning];
//    // Dispose of any resources that can be recreated.
//}
//
///*
//#pragma mark - Navigation
//
//// In a storyboard-based application, you will often want to do a little preparation before navigation
//- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
//    // Get the new view controller using [segue destinationViewController].
//    // Pass the selected object to the new view controller.
//}
//*/
//
//@end




